<?php
class ModelCommunityEthos extends Model {
	
	public function getEthos($ethos_id){
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_ethos` where ethos_id='".(int)$ethos_id."'";
		return $this->db->query($sql)->row;
	}
	
	public function addEthos($data= array()){
		$abpicUrl = "";
		if ($data['pic_url'] && file_exists(DIR_IMAGE . $data['pic_url'])) {
			$abpicUrl =  'image/'.$data['pic_url'];
		} 
		$this->db->query("insert into `".DB_PREFIX."config_ethos` set title='".$data['title']."',description='".$data['description']."',pic_url='".$data['pic_url']."',ab_pic_url='".$abpicUrl."',url='".$data['url']."',url_type='".(int)$data['url_type']."',summary='".$data['summary']."'");
		$lastArticleId = (int)$this->db->getLastId();
		if(!$data['url_type']){
			$url = "index.php?route=".$data['url'].'&company_id='.(int)$lastArticleId;
			$this->db->query("update `".DB_PREFIX."config_ethos` set url_type='".(int)$data['url_type']."',url='".$url."' where ethos_id='".(int)$lastArticleId."'");
		}
	}
	
	public function editEthos($ethos_id,$data=array()){
		if(strpos($data['url'], "index.php")===0){
			$url = $data['url'];
		}else{
			$url = "index.php?route=".$data['url'].'&ethos_id='.(int)$ethos_id;
		}
		$abpicUrl = "";
		if ($data['pic_url'] && file_exists(DIR_IMAGE . $data['pic_url'])) {
			$abpicUrl =  'image/'.$data['pic_url'];
		} 
		$sql = "update ".DB_PREFIX."config_ethos set title='".$data['title']."',description='".$data['description']."',pic_url='".$data['pic_url']."',ab_pic_url='".$abpicUrl."',url='".$url."',url_type='".(int)$data['url_type']."',summary='".$data['summary']."' where ethos_id='".(int)$ethos_id."'";
		$this->db->query($sql);
	}
	
	public function deleteEthos($ethos_id){
		$sql ="delete  from ".DB_PREFIX."config_ethos where ethos_id='".(int)$ethos_id."'";
		$this->db->query($sql);
	}
	
	
	
	public function getTotalEthoss() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_ethos`");
		return $query->row['total'];
	}
	
	private function filterSql($data){
		$sql ="";
		
		if(isset($data['filter_title'])&&!empty($data['filter_title'])){
			$sql .= " and title like '%".$data['filter_title']."%'";
		}
		
		if(isset($data['filter_news_id'])&&!empty($data['filter_news_id'])){
			$sql .= " and news_id='".(int)$data['filter_news_id']."'";
		}
		
		return $sql;
		
	}
	
	public function getEthoss($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_ethos`  where 1=1";

		$sql .=$this->filterSql($data);
		
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	
	
}
?>